{% extends "base.html" %}

{% block title %}个人中心{% endblock %}

{% block content %}
<div class="container py-4">
    <!-- 欢迎卡片 -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card bg-gradient" style="background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);">
                <div class="card-body text-white p-4">
                    <div class="d-flex justify-content-between align-items-start mb-3">
                        <h2 class="card-title mb-0">欢迎回来, {{ current_user.username }}!</h2>
                        <a href="{{ url_for('profile') }}" class="btn btn-light btn-sm d-flex align-items-center">
                            <i class="bi bi-person-gear me-1"></i>
                            个人资料设置
                        </a>
                    </div>
                    <div class="d-flex align-items-center">
                        <div class="display-4 me-3">
                            <i class="bi bi-coin"></i>
                        </div>
                        <div>
                            <h3 class="mb-0">当前积分</h3>
                            <h2 class="display-4 mb-0">{{ current_user.points }}</h2>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 功能卡片 -->
    <div class="row g-4">
        <!-- 积分兑换 -->
        <div class="col-md-6">
            <div class="card h-100 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="feature-icon bg-primary bg-gradient text-white me-3 p-3 rounded">
                            <i class="bi bi-shop"></i>
                        </div>
                        <h4 class="mb-0">积分兑换</h4>
                    </div>
                    <p class="text-muted">浏览可兑换的商品并使用您的积分兑换心仪的物品。</p>
                    <a href="{{ url_for('products') }}" class="btn btn-outline-primary">
                        去兑换 <i class="bi bi-arrow-right"></i>
                    </a>
                </div>
            </div>
        </div>

        <!-- 积分记录 -->
        <div class="col-md-6">
            <div class="card h-100 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="feature-icon bg-success bg-gradient text-white me-3 p-3 rounded">
                            <i class="bi bi-clock-history"></i>
                        </div>
                        <h4 class="mb-0">积分记录</h4>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>时间</th>
                                    <th>类型</th>
                                    <th>积分变动</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for record in point_records %}
                                <tr>
                                    <td>{{ record.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                                    <td>{{ record.record_type }}</td>
                                    <td class="text-success">+{{ record.points }}</td>
                                </tr>
                                {% else %}
                                <tr>
                                    <td colspan="3" class="text-center text-muted">暂无积分记录</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- 环保贡献 -->
        <div class="col-md-6">
            <div class="card h-100 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="feature-icon bg-info bg-gradient text-white me-3 p-3 rounded">
                            <i class="bi bi-tree"></i>
                        </div>
                        <h4 class="mb-0">环保贡献</h4>
                    </div>
                    <div class="text-center py-3">
                        <h2 class="display-4 text-success mb-0">{{ current_user.cigarette_butts }}</h2>
                        <p class="text-muted">回收烟蒂数量（个）</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 排行榜 -->
        <div class="col-md-6">
            <div class="card h-100 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="feature-icon bg-warning bg-gradient text-white me-3 p-3 rounded">
                            <i class="bi bi-trophy"></i>
                        </div>
                        <h4 class="mb-0">积分排行榜</h4>
                    </div>
                    <div class="list-group list-group-flush">
                        {% for user in top_users %}
                            <a href="{{ url_for('user_detail', user_id=user.id) }}" 
                               class="list-group-item list-group-item-action">
                                <div class="d-flex align-items-center">
                                    <div class="position-relative me-3">
                                        {% if user.avatar_data %}
                                            <img src="{{ url_for('get_avatar', user_id=user.id) }}" 
                                                 class="rounded-circle" 
                                                 alt="头像" 
                                                 style="width: 40px; height: 40px; object-fit: cover;">
                                        {% else %}
                                            <img src="{{ url_for('static', filename=user.avatar) }}" 
                                                 class="rounded-circle" 
                                                 alt="头像" 
                                                 style="width: 40px; height: 40px; object-fit: cover;">
                                        {% endif %}
                                        <span class="position-absolute top-0 start-0 translate-middle badge 
                                                     {% if loop.index == 1 %}bg-warning{% 
                                                     elif loop.index == 2 %}bg-secondary{% 
                                                     elif loop.index == 3 %}bg-bronze{% 
                                                     else %}bg-light text-dark{% endif %} border">
                                            {{ loop.index }}
                                        </span>
                                    </div>
                                    <div class="flex-grow-1">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <div>
                                                {{ user.username }}
                                                {% if user.username == current_user.username %}
                                                    <span class="badge bg-info ms-2">我</span>
                                                {% endif %}
                                            </div>
                                            <span class="badge bg-primary rounded-pill">{{ user.points }}分</span>
                                        </div>
                                        <small class="text-muted">已回收 {{ user.cigarette_butts }} 个烟蒂</small>
                                    </div>
                                </div>
                            </a>
                        {% else %}
                            <div class="text-center text-muted py-3">
                                暂无排名数据
                            </div>
                        {% endfor %}
                        {% if top_users|length >= 10 %}
                            <div class="text-center mt-3">
                                <a href="{{ url_for('leaderboard') }}" class="btn btn-outline-primary btn-sm">
                                    查看完整排行榜 <i class="bi bi-arrow-right"></i>
                                </a>
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
<style>
    .feature-icon {
        width: 48px;
        height: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
    }

    .card {
        transition: transform 0.2s;
    }

    .card:hover {
        transform: translateY(-5px);
    }

    .bg-gradient {
        background-size: 200% 200%;
        animation: gradient 15s ease infinite;
    }

    @keyframes gradient {
        0% {
            background-position: 0% 50%;
        }
        50% {
            background-position: 100% 50%;
        }
        100% {
            background-position: 0% 50%;
        }
    }
</style>
{% endblock %} 